# Makefile
# $DateTime$
# $Id: Makefile 146 2011-10-22 02:09:07Z gian.james $

TARGET = sanctioned
ST_TARGET = lib$(TARGET).a
SH_TARGET = lib$(TARGET).so

SRC = common.cpp hash.cpp object.cpp cmd.cpp character.cpp \
	weapon.cpp traits.cpp stat.cpp container.cpp Range.cpp \
	condition.cpp Socket.cpp skill.cpp agency.cpp feat.cpp \
	magic.cpp log.cpp npc.cpp item.cpp protocol.cpp nlp.cpp \
	ansi.cpp professions.cpp training.cpp client.cpp \
	module.cpp mission.cpp \
	character-odb.cxx object-odb.cxx


OPT = 0
#DEBUG = dwarf-2
DEBUG = 3

OBJDIR  = obj
INCDIRS = /usr/lib/boost_1_47_0
LIBDIRS =
XTRALIB = 

CPPSTD = -std=c++0x
CPPDEFS = 
CPPFLAGS = -g$(DEBUG)
CPPFLAGS += -O$(OPT)
CPPFLAGS += $(CPPDEFS)
CPPFLAGS += -Wall
CPPFLAGS += -Wundef
#CPPFLAGS += -fshort-enums
#CPPFLAGS += -fno-exceptions
#CPPFLAGS += -fno-rtti
CPPFLAGS += -fPIC
CPPFLAGS += $(patsubst %,-I%,$(INCDIRS))

#LIBDEFS = -lsocket -lnsl
#LIBDEFS = -lsocket -lnsl -lstdc++
LIBDEFS = -lodb-mysql -lodb
LDFLAGS = $(patsubst %,-L%,$(LIBDIRS))

GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d

ALLDEFS = $(CPPFLAGS) $(GENDEPFLAGS)

OBJ = $(SRC:%.cpp=$(OBJDIR)/%.o)

CC = gcc
AR = ar rcs
RM = rm -rf
ODB = /opt/odb/bin/odb

#all: begin $(ST_TARGET) $(SH_TARGET) end
all: begin begin_odb do_odb $(ST_TARGET) $(SH_TARGET) end

begin:
	@echo
	@echo "Building library: libsanctioned.*"

begin_odb:
	@echo
	@echo "Building ODB constituents..."

end:
	@echo
	@echo "Finished building library"

# Create library from object files.

# TODO Remove --generate-schema for actual DB
do_odb:
	$ODB --generate-query --generate-schema -d mysql character.hpp
	$ODB --generate-query --generate-schema -d mysql object.hpp
	

.SECONDARY : $(ST_TARGET)
.PRECIOUS : $(OBJ)
libsanctioned.a: $(OBJ)
	$(AR) $@ $(OBJ)

.SECONDARY : $(SH_TARGET)
.PRECIOUS : $(OBJ)
%.so: $(OBJ)
	$(CC) -shared -o $@ -fPIC $(OBJ) $(LIBDEFS)
		
$(OBJDIR)/%.o: %.cpp
	$(CC) -c $(ALLDEFS) $(CPPSTD) $< -o $@

# Target: clean project.
clean:
	$(RM) $(ST_TARGET)
	$(RM) $(SH_TARGET)
	$(RM) $(OBJDIR)/*
	$(RM) $(SRC:%.cpp=%.d)
	$(RM) .dep


# Create object files directory
$(shell mkdir $(OBJDIR) 2>/dev/null)


# Include the dependency files.
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)

